const axios = require('axios');


async function getSensorsData(req, res, next){
    try {
        const loginResponse = await axios.post('http://api.nlecloud.com/Users/Login', {
            Account: '15056535392',
            Password: 'cggcycml1',
            IsRememberMe:true
        });
        // console.log(loginResponse?.data);
        const token = loginResponse?.data?.ResultObj?.AccessToken;

        let data = JSON.stringify({
        "PageSize": 3000
        });
        let config = {
        method: 'get',
        maxBodyLength: Infinity,
        url: 'http://api.nlecloud.com/devices/Datas?devIds=1218303',
        headers: { 
            'Accesstoken': token,
            'Content-Type': 'application/json', 
        },
        data : data
        };

        const sensorDataRes = await axios.request(config);
        const sensorData = sensorDataRes?.data?.ResultObj[0]?.Datas;

        const latestSensorData = {};
        sensorData.forEach((data) => {
            latestSensorData[data.ApiTag] = data.Value;
        });
        
        // console.log(sensorDataRes?.data?.ResultObj[0].Datas);
        // console.log(latestSensorData);
        res.send(  latestSensorData ); 
    } catch (error) {
        console.log(error)
        res.status(500).send({error});
    }

}

module.exports = {
    getSensorsData,
};